/*
Copyright (c) 2012, Janez Žemva
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
o The names of contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once
#ifndef ETL_UTILITY_HPP
# define ETL_UTILITY_HPP

#include <cstring>

#include <iterator>

#include <typeinfo>

#include <type_traits>

namespace etl
{
  template <typename T, typename U>
  using sum_type = decltype(::std::declval<T>() + ::std::declval<U>());

  template <typename T, typename U>
  using diff_type = decltype(::std::declval<T>() - ::std::declval<U>());

  template <typename T, typename U>
  using prod_type = decltype(::std::declval<T>() * ::std::declval<U>());

  template <typename T, typename U>
  using quot_type = decltype(::std::declval<T>() / ::std::declval<U>());

  namespace detail
  {
    template <::std::size_t N, ::std::size_t E>
    struct map_extent_ : ::std::conditional<E >= N,
      ::std::integral_constant<decltype(E), E>,
      map_extent_<N, 2 * E> >::type
    {
    };

    template <::std::size_t N>
    struct map_extent : map_extent_<N, 1>
    {
    };

    template <bool B>
    using bool_ = ::std::integral_constant<bool, B>;

    template <class Head, class... Tail>
    struct all_of : bool_<Head::value && all_of<Tail...>::value> {};

    template <class Head>
    struct all_of<Head> : bool_<Head::value> { };

    template <::std::size_t...> struct indices { };

    template <::std::size_t M, ::std::size_t... Is>
    struct make_indices : make_indices<M - 1, M - 1, Is...> {};

    template <::std::size_t... Is>
    struct make_indices<0, Is...> : indices<Is...> {};

    template <typename ...T> struct list {};

    template <typename T, typename U>
    struct pair
    {
      typedef T first;
      typedef U second;
    };

    template <::std::size_t I>
    using index = ::std::integral_constant<::std::size_t, I>;

    template <::std::size_t ON, ::std::size_t M, ::std::size_t N,
      typename ...Ps>
    struct cartesian : ::std::conditional<bool(N),
      cartesian<ON, M, N - 1, pair<index<M>, index<N> >, Ps...>,
      cartesian<ON, M - 1, ON, pair<index<M>, index<N> >, Ps...>
    >::type
    {
    };

    template <::std::size_t ON, typename ...Ps>
    struct cartesian<ON, 0, 0, Ps...> :
      list<pair<index<0>, index<0> >, Ps...>
    {
    };

    template <::std::size_t M, ::std::size_t N>
    struct make_cartesian : cartesian<N - 1, M - 1, N - 1>
    {
      static_assert(M > 0, "M has to be greater than 0");
      static_assert(N > 0, "N has to be greater than 0");
    };

    template <typename T, typename U, typename = void>
    struct is_container : ::std::false_type { };

    template <typename T, typename U>
    struct is_container<T, U,
      typename ::std::enable_if<
        ::std::is_same<T, typename U::value_type>{} &&
        ::std::is_same<typename U::const_iterator,
          decltype(::std::begin(::std::declval<U const>()))>{}
      >::type
    > : ::std::true_type { };
  }
}

#endif // ETL_UTILITY_HPP
